const Trie = require("../template")

function testTrieComplexScenarios() {
  const trie = new Trie()

  console.log(
    "1. 自动补全场景测试:",
    (() => {
      const autocompleteTrie = new Trie()
      const words = ["apple", "application", "apply", "app", "approach", "approve"]
      words.forEach(word => autocompleteTrie.insert(word))
      
      const suggestions = autocompleteTrie.getWordsWithPrefix("app")
      const expected = ["apple", "application", "apply", "app", "approach", "approve"]
      return JSON.stringify(suggestions.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "2. 拼写检查场景测试:",
    (() => {
      const spellCheckTrie = new Trie()
      const dictionary = ["hello", "world", "javascript", "programming", "algorithm"]
      dictionary.forEach(word => spellCheckTrie.insert(word))
      
      const correctWords = ["hello", "world", "javascript"]
      const incorrectWords = ["helo", "worl", "javascrip"]
      
      const correctResults = correctWords.every(word => spellCheckTrie.search(word))
      const incorrectResults = incorrectWords.every(word => !spellCheckTrie.search(word))
      
      return correctResults && incorrectResults
    })()
  )

  console.log(
    "3. 路由表场景测试:",
    (() => {
      const routingTrie = new Trie()
      const routes = ["/api/users", "/api/posts", "/api/comments", "/admin/dashboard", "/admin/settings"]
      routes.forEach(route => routingTrie.insert(route))
      
      const apiRoutes = routingTrie.getWordsWithPrefix("/api")
      const adminRoutes = routingTrie.getWordsWithPrefix("/admin")
      
      return apiRoutes.length === 3 && adminRoutes.length === 2
    })()
  )

  console.log(
    "4. 文件系统场景测试:",
    (() => {
      const fileSystemTrie = new Trie()
      const files = ["/home/user/documents/file1.txt", "/home/user/documents/file2.txt", 
                    "/home/user/pictures/photo1.jpg", "/home/user/pictures/photo2.jpg"]
      files.forEach(file => fileSystemTrie.insert(file))
      
      const documentFiles = fileSystemTrie.getWordsWithPrefix("/home/user/documents")
      const pictureFiles = fileSystemTrie.getWordsWithPrefix("/home/user/pictures")
      
      return documentFiles.length === 2 && pictureFiles.length === 2
    })()
  )

  console.log(
    "5. 联系人搜索场景测试:",
    (() => {
      const contactsTrie = new Trie()
      const contacts = ["John Smith", "Jane Doe", "John Doe", "Bob Johnson", "Alice Brown"]
      contacts.forEach(contact => contactsTrie.insert(contact))
      
      const johnContacts = contactsTrie.getWordsWithPrefix("John")
      const janeContacts = contactsTrie.getWordsWithPrefix("Jane")
      
      return johnContacts.length === 2 && janeContacts.length === 1
    })()
  )

  console.log(
    "6. 多语言支持测试:",
    (() => {
      const multiLangTrie = new Trie()
      const words = ["hello", "hola", "bonjour", "こんにちは", "你好"]
      words.forEach(word => multiLangTrie.insert(word))
      
      const englishWords = multiLangTrie.getWordsWithPrefix("h")
      const allWords = multiLangTrie.getAllWords()
      
      return englishWords.length === 1 && allWords.length === 5
    })()
  )

  console.log(
    "7. 动态更新场景测试:",
    (() => {
      const dynamicTrie = new Trie()
      dynamicTrie.insert("initial")
      dynamicTrie.insert("data")
      
      // 模拟动态添加
      dynamicTrie.insert("new")
      dynamicTrie.insert("content")
      
      // 模拟动态删除
      dynamicTrie.delete("initial")
      
      const remainingWords = dynamicTrie.getAllWords()
      const expected = ["data", "new", "content"]
      
      return JSON.stringify(remainingWords.sort()) === JSON.stringify(expected.sort())
    })()
  )

  console.log(
    "8. this上下文复杂场景测试:",
    (() => {
      const complexObj = {
        language: "en",
        processComplex: function(words, prefix) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          const prefixWords = trie.getWordsWithPrefix(prefix)
          return prefixWords.length > 0 ? this.language : "unknown"
        }
      }
      return complexObj.processComplex(["hello", "world"], "h") === "en"
    })()
  )

  console.log(
    "9. 复杂对象this绑定复杂场景测试:",
    (() => {
      const complexConfigObj = {
        config: { maxResults: 3 },
        processWithConfig: function(words, prefix) {
          const trie = new Trie()
          words.forEach(word => trie.insert(word))
          const prefixWords = trie.getWordsWithPrefix(prefix)
          return prefixWords.slice(0, this.config.maxResults)
        }
      }
      const words = ["apple", "application", "apply", "approach", "approve"]
      const result = complexConfigObj.processWithConfig(words, "app")
      return result.length === 3
    })()
  )

  console.log(
    "10. 复杂场景一致性测试:",
    (() => {
      const consistencyTrie = new Trie()
      const words = ["test", "testing", "tester", "tested"]
      words.forEach(word => consistencyTrie.insert(word))
      
      const searchResult = consistencyTrie.search("test")
      const prefixResult = consistencyTrie.startsWith("test")
      const allWords = consistencyTrie.getAllWords()
      const prefixWords = consistencyTrie.getWordsWithPrefix("test")
      
      return searchResult === true &&
             prefixResult === true &&
             allWords.length === 4 &&
             prefixWords.length === 4
    })()
  )

  console.log(
    "11. 复杂场景边界测试:",
    (() => {
      const boundaryTrie = new Trie()
      boundaryTrie.insert("")
      boundaryTrie.insert("a")
      boundaryTrie.insert("aa")
      boundaryTrie.insert("aaa")
      
      const emptyWords = boundaryTrie.getWordsWithPrefix("")
      const singleWords = boundaryTrie.getWordsWithPrefix("a")
      const doubleWords = boundaryTrie.getWordsWithPrefix("aa")
      
      return emptyWords.length === 4 &&
             singleWords.length === 3 &&
             doubleWords.length === 2
    })()
  )

  console.log(
    "12. 复杂场景性能测试:",
    (() => {
      const perfTrie = new Trie()
      const words = Array.from({ length: 1000 }, (_, i) => `word${i}`)
      words.forEach(word => perfTrie.insert(word))
      
      const start = performance.now()
      const allWords = perfTrie.getAllWords()
      const prefixWords = perfTrie.getWordsWithPrefix("word")
      const end = performance.now()
      
      return allWords.length === 1000 &&
             prefixWords.length === 1000 &&
             (end - start) < 1000
    })()
  )
}

testTrieComplexScenarios()
